/**
 * 表单值域字典表管理初始化
 */
var SdsQcfitemDic = {
    id: "SdsQcfitemDicTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsQcfitemDic.initColumn = function () {
    return [[
        {title: '序号', field: 'indNo', width: 50, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
            }},
        {title: '字典代码', field: 'code', width: 100, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
            }},
        {title: '字典描述', field: 'desc', width: 200, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
            }},
        {title: '有效', field: 'isActive', width: 60, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                if(value==1){
                    return "<span class='label label-success'>是</span>";
                }else{
                    return "<span class='label label-warning'>否</span>";
                }
            },editor:{
                type:'combobox',
                options:{required:true,valueField:'id',textField:'text',editable:false,
                    data:
                        [
                            {'id':1,'text':'是'},
                            {'id':0,'text':'否'}
                        ]}
            }},
        {title: '默认', field: 'isDefault', width: 60, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                if(value==1){
                    return "<span class='label label-success'>是</span>";
                }else{
                    return "<span class='label label-warning'>否</span>";
                }
            },editor:{
                type:'combobox',
                options:{required:true,valueField:'id',textField:'text',editable:false,
                    data:
                        [
                            {'id':1,'text':'是'},
                            {'id':0,'text':'否'}
                        ]}
            }},
        {title: '关键词', field: 'keyword', width: 200, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'text',
                options:{required:true}
            }}
        // ,
        // {title: '匹配规则', field: 'matchRule', width: 200, align: 'center', valign: 'middle', sortable: true, editor:{
        //         type:'text',
        //         options:{required:true}
        //     }}
    ]];
};

/**
 * 检查是否选中
 */
SdsQcfitemDic.check = function () {
    var selected = $('#' + this.id).datagrid('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsQcfitemDic.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加新一行表格
 */
SdsQcfitemDic.addFItemDic = function () {
    var rowNum=$('#' + SdsQcfitemDic.id).datagrid("getRows").length;
    //添加一行
    $('#' + SdsQcfitemDic.id).datagrid('appendRow',{
        indNo: rowNum + 1,
        code: '',
        desc: '',
        isActive: 1,
        isDefault: 1,
        matchRule: '',
        keyword: '',
        formItemId:$("#formItemId").val()
    });
    var rowIndex= rowNum;
    $('#' + SdsQcfitemDic.id).datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
};

/**
 * 保存数据到库表
 */
SdsQcfitemDic.saveFItemDic = function () {
    //保存完毕后，同步数据
    var operation_save = function(){
        SdsQcfitemDic.table.acceptChanges();//调用datagrid自带的方法，确认其修改 然后保存至数据库
        var rows =  $('#' + SdsQcfitemDic.id).datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row != undefined && row != {}) {
                row.matchRule = encodeURIComponent(row.matchRule);
                if (row.id != undefined) {
                    //update Row
                    delete row.updateDate;
                    delete row.updateUser;
                    Sys.ajax(Sys.backendPath + "/sds/sdsqcfitemdic/update", row, function (data) {
                        Sys.success("修改成功!");
                    },function(data){
                        Sys.error("修改失败!" + data.responseJSON.message + "!");
                    }, "json", false, "修改值域字典保存至库中..");
                } else {
                    //add Row
                    Sys.ajax(Sys.backendPath + "/sds/sdsqcfitemdic/add", row, function (data) {
                        Sys.success("添加成功!");
                    },function(data){
                        Sys.error("添加失败!" + data.responseJSON.message + "!");
                    }, "json", false, "新增值域字典保存至库中..");
                }
            }
        }
    };
    Sys.confirm("是否保存表格数据至数据库?",operation_save);
};

/**
 * 删除行同时删除数据库表数据
 */
SdsQcfitemDic.delFItemDic = function () {
    if (SdsQcfitemDic.check()) {
        var operation = function(){
            if(SdsQcfitemDic.seItem.id!=null&&SdsQcfitemDic.seItem.id!=""){
                var ajax = new $ax(Sys.backendPath + "/sds/sdsqcfitemdic/setInvalidStatus", function (data) {
                    Sys.success("删除成功!");
                    SdsQcfitemDic.table.refresh();
                }, function (data) {
                    Sys.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("sdsQcfitemDicId",SdsQcfitemDic.seItem.id);
                ajax.start();
            }else{
                SdsQcfitemDic.table.delRow();
            }
        };
        Sys.confirm("是否删除该行数据?",operation);
    }
};

/**
 * 查询表单值域字典表列表
 */
SdsQcfitemDic.search = function () {
    var queryData = {};
    queryData['formItemId'] = $("#formItemId").val();
    SdsQcfitemDic.table.refresh({queryParams: queryData});
};

$(function () {
    var defaultColunms = SdsQcfitemDic.initColumn();
    var table = new HISUITable(SdsQcfitemDic.id, Sys.backendPath + "/sds/sdsqcfitemdic/list", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-90);
    SdsQcfitemDic.table = table.init();
});
